package com.suishoupai.xmb.Security;

import com.suishoupai.xmb.dao.CutBlacklistMapper;
import com.suishoupai.xmb.dao.CutUserMapper;
import com.suishoupai.xmb.entity.Blacklist;
import com.suishoupai.xmb.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;


/**
 * Created by caojian on 2017/3/21.
 */

public class MyUserDetailService implements UserDetailsService {
    @Autowired
    private CutUserMapper cutUserMapper;
    @Autowired
    private CutBlacklistMapper cutBlacklistMapper;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = cutUserMapper.loadUserByEmail(username);
        if (user == null) {
            throw new UsernameNotFoundException("user==null");
        }
        Blacklist blacklist = cutBlacklistMapper.selectByPrimaryKey(user.getId());
        if (blacklist != null) {
            throw new UsernameNotFoundException("账号(" + username + ")已被系统冻结！(原因：" + blacklist.getDescription() + ")");
        }
        return new MyUserDetail(user);
    }
}
